import * as types from '../constants/incomeTypes'
import API from '../../api/request'

export function income () {
  return dispatch => {
    getIncomeStat(dispatch)
  }
}

// 获取收入汇总数据
async function getIncomeStat (dispatch) {
  let url = '/api/user/each_income_total'
  let stat = ''
  let res = await API.get(url)
  if (global.isApiFetchingSuccess(res)) {
    // TODO res.data
    stat = res.data && res.data.data
    dispatch(_apiGetSuccess(stat))
  } else {
    dispatch(_apiGetError(res.message))
  }
}

function _apiGetSuccess (income) {
  return {
    type: types.INCOME_IN_SUCCESS,
    income
  }
}

function _apiGetError (err) {
  return {
    type: types.INCOME_IN_ERROR,
    err
  }
}
